package org.sachiew;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
 * Raw Date ==> hashmap & hashset ==> Network structure
 */
public class MyNetworkMap {
    private HashMap<String, HashSet<String>> networksHashMap = new HashMap<String, HashSet<String>>();
    private ArrayList<MyLink> linksArray;

    public MyNetworkMap(ArrayList<MyLink> linksArray) {
        this.linksArray = linksArray;
    }

    /**
     * 暴露数据的网络结构对象
     * 
     * @return networksHashMap
     */
    public HashMap<String, HashSet<String>> getNetworksHashMap() {
        return networksHashMap;
    }

    /**
     * 数据映射为网络结构
     */
    public void map() {
        for (MyLink myLink : this.linksArray) {
            if (this.networksHashMap.containsKey(myLink.getMainPoint())) {
                this.networksHashMap.get(myLink.getMainPoint()).add(myLink.getAttachPoint());
            } else {
                this.networksHashMap.put(myLink.getMainPoint(), new HashSet<String>());
                this.networksHashMap.get(myLink.getMainPoint()).add(myLink.getAttachPoint());
            }
        }
    }

    /**
     * 打印网络结构
     */
    public void displayNetwork() {
        for (String key : this.networksHashMap.keySet()) {
            // Iterator<String> iterator = this.networksHashMap.get(key).iterator();
            System.out.println(
                    "---------------节点 " + key + " 度为 " + this.networksHashMap.get(key).size() + " ---------------");
            System.out.println(this.networksHashMap.get(key) + "\n");
            // while (iterator.hasNext()) {
            // System.out.print(iterator.next() + " ");
            // }
            // System.out.println('\n');
        }
    }
}